﻿#include "DialogTotalQuantityCalculation.h"
#include "ui_DialogTotalQuantityCalculation.h"

#include "AnimationSettingData.h"
#include "DataChosenListDialog.h"
#include <QIntValidator>
#include "IdaWidgetHelper.h"
#include <QListView>
#include <QDebug>

namespace pst
{
    DialogTotalQuantityCalculation::DialogTotalQuantityCalculation(QWidget* parent)
        : IdaDialogFrame(parent), ui(new Ui::DialogTotalQuantityCalculation())
    {
        ui->setupUi(this);

        setWindowTitle(tr("总量计算"));
        IdaWidgetHelperInstance->setMainButtonStyle(ui->pushButton_confirm); // 将确定设置为主按钮

        ui->textEdit_Ex->setStyleSheet("QTextEdit { border: none; }");
        ui->textEdit_Ey->setStyleSheet("QTextEdit { border: none; }");
        ui->textEdit_Ez->setStyleSheet("QTextEdit { border: none; }");
        ui->textEdit_Ex->setFixedHeight(100);
        ui->textEdit_Ey->setFixedHeight(100);
        ui->textEdit_Ez->setFixedHeight(100);

        ui->pushButton_Ex->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Ex->setStyleSheet("QPushButton { text-align: center; }");
        ui->pushButton_Ey->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Ey->setStyleSheet("QPushButton { text-align: center; }");
        ui->pushButton_Ez->setIcon(QIcon(":/rc/apostrophe.png"));
        ui->pushButton_Ez->setStyleSheet("QPushButton { text-align: center; }");

        connect(ui->pushButton_Ex, &QPushButton::clicked, this, &DialogTotalQuantityCalculation::slot_selectDataButtonExClicked);
        connect(ui->pushButton_Ey, &QPushButton::clicked, this, &DialogTotalQuantityCalculation::slot_selectDataButtonEyClicked);
        connect(ui->pushButton_Ez, &QPushButton::clicked, this, &DialogTotalQuantityCalculation::slot_selectDataButtonEzClicked);
    }

    DialogTotalQuantityCalculation::~DialogTotalQuantityCalculation()
    {
        delete ui;
    }

    void DialogTotalQuantityCalculation::setCurveNameList(const QList<QString>& curveNameList)
    {
        m_curveNameList = curveNameList;
    }

    void DialogTotalQuantityCalculation::on_pushButton_confirm_clicked()
    {
        emit signal_totalCalculation(ui->textEdit_Ex->toPlainText().split("\n", QString::SkipEmptyParts),
                                     ui->textEdit_Ey->toPlainText().split("\n", QString::SkipEmptyParts),
                                     ui->textEdit_Ez->toPlainText().split("\n", QString::SkipEmptyParts));
        this->close();
    }

    void DialogTotalQuantityCalculation::on_pushButton_cancel_clicked()
    {
        this->close();
    }

    void DialogTotalQuantityCalculation::slot_sentChosenDataIds(const QModelIndexList& chosenID)
    {
        m_selectedIndexs.clear();
        auto count = chosenID.count();
        QString strText{};
        for (int i = 0; i < count; ++i)
        {
            auto chosenData = chosenID.at(i);
            if (chosenData.isValid())
            {
                auto str = chosenData.data().toString();
                strText += str + "\n";

                auto index = chosenID.at(i).row();
                m_selectedIndexs.append(index);
            }
        }
        auto list = strText.split("\n", QString::SkipEmptyParts);
        for (const auto& str : list)
        {
            auto xList = ui->textEdit_Ex->toPlainText().split("\n", QString::SkipEmptyParts);
            if (xList.contains(str))
            {
                xList.removeOne(str);
                ui->textEdit_Ex->setText(xList.join("\n"));
                continue;
            }

            auto yList = ui->textEdit_Ey->toPlainText().split("\n", QString::SkipEmptyParts);
            if (yList.contains(str))
            {
                yList.removeOne(str);
                ui->textEdit_Ey->setText(yList.join("\n"));
                continue;
            }

            auto zList = ui->textEdit_Ez->toPlainText().split("\n", QString::SkipEmptyParts);
            if (zList.contains(str))
            {
                zList.removeOne(str);
                ui->textEdit_Ez->setText(zList.join("\n"));
                continue;
            }
        }
        if (m_isExPushButtonClicked)
            ui->textEdit_Ex->setText(strText);
        if (m_isEyPushButtonClicked)
            ui->textEdit_Ey->setText(strText);
        if (m_isEzPushButtonClicked)
            ui->textEdit_Ez->setText(strText);
    }

    void DialogTotalQuantityCalculation::openDataChosenListDialog()
    {
        m_dataChosenListDialog = new DataChosenListDialog();
        m_dataChosenListDialog->setFixedSize(300, 280);
        // m_dataChosenListDialog->moveToCenter();
        m_dataChosenListDialog->setData(m_curveNameList);
        m_dataChosenListDialog->updateUI();
        m_dataChosenListDialog->showWindow();
        m_dataChosenListDialog->setSelctedIndexs(m_selectedIndexs); // 默认选中第一个

        connect(m_dataChosenListDialog,
                &DataChosenListDialog::signal_sentChosenDataIds,
                this, &DialogTotalQuantityCalculation::slot_sentChosenDataIds);
    }

    void DialogTotalQuantityCalculation::slot_selectDataButtonExClicked()
    {
        m_isExPushButtonClicked = true;
        m_isEyPushButtonClicked = false;
        m_isEzPushButtonClicked = false;
        openDataChosenListDialog();
    }
    void DialogTotalQuantityCalculation::slot_selectDataButtonEyClicked()
    {
        m_isExPushButtonClicked = false;
        m_isEyPushButtonClicked = true;
        m_isEzPushButtonClicked = false;
        openDataChosenListDialog();
    }
    void DialogTotalQuantityCalculation::slot_selectDataButtonEzClicked()
    {
        m_isExPushButtonClicked = false;
        m_isEyPushButtonClicked = false;
        m_isEzPushButtonClicked = true;
        openDataChosenListDialog();
    }

} // namespace pst
